package com.cy.uv.controller;

import com.cy.uv.entity.Member;
import com.cy.uv.feign.JwtService;
import com.cy.uv.mapper.MemberMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "MemberController", description = "会员管理接口")
@RestController
@RequestMapping(value = "/api/members")
public class MemberController {
    @Autowired
    private com.cy.uv.service.MemberService memberService;

    @Autowired
    private com.cy.uv.feign.JwtService jwtService;
/*

    @RequestMapping(value = "/add")
    @Operation(summary = "添加会员",  description = "添加一个新的会员到数据库，注册用")
    @ApiResponse(responseCode = "200", description = "存储成功")
    @ApiResponse(responseCode = "400", description = "存储失败")
    public ResponseEntity<Member> addMember(@PathVariable Member member) {
        boolean isSaved = memberService.save(member);
        return ResponseEntity.ok(isSaved ? member : null);
    }
*/
/*

    @RequestMapping(value = "/delete")
    @Operation(summary ="注销会员",description = "个人认为该方法不会启用，需要改为逻辑删除")
     public String deleteMember() {
        return "deleteMember";
    }

*/

    /*@RequestMapping(value = "/update")
    @Operation(summary = "更新会员信息",description = "更新会员信息，弃用，不会给出使用")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @ApiResponse(responseCode = "400", description = "更新失败")
    public ResponseEntity<Member> updateMember(@PathVariable Member member) {
        boolean isUpdated = memberService.updateById(member);
        try {
            memberService.updateById(member);
        }catch (Exception e){
            return ResponseEntity.badRequest().build();
        }

        return ResponseEntity.ok(isUpdated ? member : null);
    }*/
    @RequestMapping(value = "/get")
    @ApiResponse(responseCode = "200", description = "获取成功")
    @Operation(summary = "获取会员信息",description = "获取会员信息，用在管理上？知道现在管理模块都没出来·····")
    public String getMember() {
        return "getMember";
    }
    @Operation(summary = "获取所有member的信息",description = "感觉不会使用")
    @ApiResponse(responseCode = "200" ,description ="所有会员信息获取成功")
    @ApiResponse(responseCode = "400" ,description = "获取失败")
    @RequestMapping(value = "/list")
    public ResponseEntity<List<Member>> getMemberList(){
        //返回mybatis查到的所有数据
        List<Member> list = memberService.getMembers();
        return ResponseEntity.ok(list);

    }
    @PostMapping(value = "/login")
    @Operation(summary = "用户登录", description = "验证用户凭证并返回JWT token")
    @ApiResponse(responseCode = "200", description = "登录成功")
    @ApiResponse(responseCode = "401", description = "登录失败")
    /*public String login(@RequestParam String name, @RequestParam String password) {
        Member loggedInMember = memberService.login(name, password);
        if (loggedInMember != null) {
            return "登录成功";
        } else {
            return "用户名或密码错误";
        }
    }*/
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginData) {
        String username = loginData.get("username");
        String password = loginData.get("password");

        if (memberService.isValidUser(username, password)) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", username);

            // 创建一个包含 username 和 claims 的 Map
            Map<String, Object> payload = new HashMap<>();
            payload.put("username", username);
            payload.put("claims", claims);

            String token = jwtService.createToken(payload);

            Map<String, String> response = new HashMap<>();
            response.put("token", token);

            return ResponseEntity.ok(response);
        } else {
            return ResponseEntity.status(401).body("Invalid username or password");
        }
    }
    @Operation(summary = "用户注册")
    @ApiResponse(responseCode = "200", description = "注册成功")
    @ApiResponse(responseCode = "400", description = "注册失败")

    @PostMapping("/register")
    public String register(@RequestBody Member member) {
        if (memberService.isNameExist(member.getUsername())) {
            return "该用户名已存在";
            /*return ResponseEntity.badRequest().body("该用户名已存在");
            * 有一说一，这样是不是很没有用户体验，搞错了还要返回重新来，不能重新原页面提交？*/
        }
        if (memberService.save(member)) {
            return "注册成功";
        } else {
           /* return ResponseEntity.badRequest().body("注册失败" );*/
            return "注册失败";
        }
    }

    @PostMapping("/changePassword")
    public String changePassword(@RequestParam String name, @RequestParam String oldPassword, @RequestParam String newPassword) {
        // 验证用户名和旧密码是否匹配
        Member member = memberService.login(name, oldPassword);
        if (member == null) {
            return "用户名或旧密码错误";
        }
        // 更新密码
        member.setPassword(newPassword);
        if (memberService.updateById(member)) {
            return "密码修改成功";
        } else {
            return "密码修改失败";
        }
    }




}
